/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.gar.bc;

import edu.upn.sigemfi.gar.be.EstadoGarantia;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Freddy
 */
public class EstadoGarantiaDelegate {

    private EstadoGarantiaFacadeLocal local;

    public EstadoGarantiaDelegate(){
        local = lookupEstadoGarantiaFacade();
    }

    private EstadoGarantiaFacadeLocal lookupEstadoGarantiaFacade() {
        try {
            Context c = new InitialContext();
            return (EstadoGarantiaFacadeLocal) c.lookup("java:comp/env/EstadoGarantiaFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    public void crear(EstadoGarantia estadoGarantia) throws Exception
    {
        try{
            local.create(estadoGarantia);
        }
        catch(Exception ex){
            local=lookupEstadoGarantiaFacade();
            throw new Exception("Crear Estado Garantia"+ex.getMessage());
        }
    }

    public void editar(EstadoGarantia estadoGarantia) throws Exception
    {
        try{
            local.edit(estadoGarantia);
        }catch (Exception ex){
            local=lookupEstadoGarantiaFacade();
            throw new Exception("Editar Estado Garantia"+ex.getMessage());
        }
    }

    public void eliminar(EstadoGarantia estadoGarantia) throws Exception
    {
        try{
            local.remove(estadoGarantia);
        }
        catch(Exception ex){
            local=lookupEstadoGarantiaFacade();
            throw new Exception("Eliminar EstadoGarantia"+ex.getMessage());
        }
    }

    public List<EstadoGarantia> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupEstadoGarantiaFacade();
            throw new Exception("Listar EstadoGarantia. " + e.getMessage(), e);
        }
    }

    public EstadoGarantia buscar(Long codigo) throws Exception {
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local = lookupEstadoGarantiaFacade();
            throw new Exception("Buscar EstadoGarantia. " + e.getMessage(), e);
        }
    }

    public List<EstadoGarantia> listarPorDNI(String DNI) throws Exception {
        try {
            return local.findActives(DNI);
        } catch (Exception e) {
            local = lookupEstadoGarantiaFacade();
            throw new Exception("Listar EstadoGarantia por DNI. " + e.getMessage(), e);
        }
    }

    public EstadoGarantia buscarPorIdGar(Long codigo) throws Exception {
        try {
            return local.findByIdGar(codigo);
        } catch (Exception e) {
            local = lookupEstadoGarantiaFacade();
            throw new Exception("Buscar EstadoGarantia. " + e.getMessage(), e);
        }
    }
}
